package com.xwaf.platform.web.controller.auth;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xwaf.platform.common.api.CommonResponse;
import com.xwaf.platform.common.api.constants.BaseConstants;
import com.xwaf.platform.common.web.BaseController;
import com.xwaf.platform.system.api.entity.DataBase;
import com.xwaf.platform.system.api.entity.auth.OperateLog;
import com.xwaf.platform.system.api.service.auth.OperateLogService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * <p>
 * 操作日志
 * <p>
 * 
 * @author 李聪 <br>
 * @email xwaf_love@yeah.net <br>
 * @since JDK 1.8<br>
 * @date 2019年12月30日 上午9:25:14 <br>
 * @see 无<br>
 *      Copyright (c) 2019, xwaf_love@yeah.net All Rights Reserved.<br>
 */
@RestController
@Api(tags = "操作日志")
@RequestMapping(value = "/m/operateLog/")
public class OperateLogController extends BaseController {

	@Autowired
	private OperateLogService operateLogService;

	@ApiOperation(value = "分页查询", response = OperateLog.class)
	@PostMapping(value = "page", produces = "application/json;charset=UTF-8")
	public CommonResponse page(@RequestParam("currentPage") String currentPage,
			@RequestParam("pageSize") String pageSize, @RequestBody OperateLog operateLog) {
		if (!StringUtils.isNotBlank(currentPage) & !StringUtils.isNotBlank(pageSize)) {
			currentPage = "1";
			pageSize = "10";
		}
		Page<OperateLog> page = new Page<OperateLog>(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
		Page<OperateLog> result = operateLogService.selPageList(page, operateLog);
		return CommonResponse.createCommonResponse(result);
	}

	@ApiOperation(value = "查询所有操作日志表", notes = "查询所有操作日志表信息", httpMethod = "POST", response = OperateLog.class, produces = MediaType.APPLICATION_JSON_VALUE)
	@PostMapping(value = "list", produces = "application/json;charset=UTF-8")
	public CommonResponse list(@RequestBody OperateLog operateLog) {
		Wrapper<OperateLog> wrapper = new EntityWrapper<OperateLog>();
		List<OperateLog> list = operateLogService.selectList(wrapper);
		return CommonResponse.createCommonResponse(list);
	}

	@ApiOperation(value = "新增操作日志表", notes = "新增操作日志表信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	@PostMapping(value = "add", produces = "application/json;charset=UTF-8")
	public CommonResponse addOperateLog(@RequestBody OperateLog operateLog) {
		operateLog.setId(DataBase.getDataBaseId(DataBase.OL));
		operateLog.setCreateBy(getLoginUser().getId());
		boolean isAdded = operateLogService.insert(operateLog);
		if (isAdded) {
			return CommonResponse.createCommonResponse(operateLog);
		} else {
			return CommonResponse.createCustomCommonResponse(BaseConstants.FAIL.getCode(), "新增失败!");
		}
	}

	@ApiOperation(value = "修改操作日志表", notes = "修改操作日志表信息", httpMethod = "PUT", produces = MediaType.APPLICATION_JSON_VALUE)
	@PutMapping(value = "update/{operateLogId}", produces = "application/json;charset=UTF-8")
	public CommonResponse updateOperateLog(@RequestBody OperateLog operateLog,
			@PathVariable("operateLogId") String operateLogId) {
		operateLog.setId(operateLogId);
		operateLog.setUpdateBy(getLoginUser().getId());
		boolean isAdded = operateLogService.updateById(operateLog);
		if (isAdded) {
			return CommonResponse.createCommonResponse();
		} else {
			return CommonResponse.createCustomCommonResponse(BaseConstants.FAIL.getCode(), "修改失败!");
		}
	}

	@ApiOperation(value = "删除操作日志表", hidden = true, notes = "删除操作日志表信息", httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
	@DeleteMapping(value = "delete/{operateLogId}", produces = "application/json;charset=UTF-8")
	public CommonResponse delById(@PathVariable("operateLogId") List<String> operateLogId) {
		// boolean isDel = operateLogService.deleteBatchIds(operateLogId);
		 boolean isDel = true;
		if (isDel) {
			return CommonResponse.createCommonResponse();
		} else {
			return CommonResponse.createCustomCommonResponse(BaseConstants.FAIL.getCode(), "删除失败!");
		}
	}

	@ApiOperation(value = "查看操作日志表详情", notes = "查看操作日志表详情信息", httpMethod = "GET", response = OperateLog.class, produces = MediaType.APPLICATION_JSON_VALUE)
	@GetMapping(value = "info/{operateLogId}", produces = "application/json;charset=UTF-8")
	public CommonResponse getInfoById(@PathVariable("operateLogId") String operateLogId) {
		OperateLog operateLog = operateLogService.selectById(operateLogId);
		return CommonResponse.createCommonResponse(operateLog);
	}
}
